import {ElMessage, ElMessageBox} from "element-plus";

const TOKEN_KEY = "dlyk_token";

/**
 * 消息提示
 *
 * @param {string} msg
 * @param {string} type - 'success', 'warning', 'info', 'error'
 */
export function messageTip(msg, type) {
    ElMessage({
        showClose: true,
        center: true,
        duration: 3000,
        message: msg,
        type: type,
    });
}

/**
 * 获取token
 *
 * @returns {string|null}
 */
export function getToken() {
    return window.sessionStorage.getItem(TOKEN_KEY) || window.localStorage.getItem(TOKEN_KEY);
}

/**
 * 删除token
 */
export function removeToken() {
    window.sessionStorage.removeItem(TOKEN_KEY);
    window.localStorage.removeItem(TOKEN_KEY);
}

/**
 * 消息确认提示
 *
 * @param {string} msg
 * @returns {Promise}
 */
export function messageConfirm(msg) {
    return ElMessageBox.confirm(
        msg,
        '系统提醒',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        removeToken();
        window.location.href = "/";
    }).catch(() => {
        messageTip("取消去登录", "warning");
    });
}
